package com.myblog.controller;

import com.myblog.constant.CodeType;
import com.myblog.mapper.ArticleMapper;
import com.myblog.service.ArticleService;
import com.myblog.service.CategoryService;
import com.myblog.service.LeaveService;
import com.myblog.utils.DataMap;
import com.myblog.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private LeaveService leaveService;


    /**
     * 后台管理中分类管理获取分类信息
     * @return
     */
    @GetMapping("/getArticleCategories")
    public String getCategories(){

        try{
            //获取分类信息
            DataMap data = categoryService.getCategories();
//            System.out.println(data.getData());
            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("CategoryController getCategories Excepion",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     * 添加或者删除分类
     * @param categoryName
     * @param type
     * @return
     */
    @PostMapping("/updateCategory")
    public String deleteCategory(@RequestParam(value = "categoryName") String categoryName, @RequestParam(value = "type")int type){
        try{
            //获取信息：添加或者删除，添加或者删除的分类名字
            DataMap data = categoryService.updataCategory(categoryName,type);

            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("CategoryController getCategories Excepion",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     * 发表博客确认框中的下拉分类表findCategoriesName
     * @return
     */
    @GetMapping("/findCategoriesName")
    public String findAllCategoriesName(){
        try{
            //获取分类名字
            DataMap data = categoryService.findAllCategoriesName();

            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("CategoryController findAllCategoriesName Excepion",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     * 分类页面获取分类和文章分类名称
     * @return
     */
    @GetMapping("/findCategoriesNameAndArticleNum")
    public String findCategoriesNameAndArticleNum(){
        try{
            //获取分类名字
            DataMap data = categoryService.findCategoriesNameAndArticleNum();

            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("CategoryController findAllCategoriesName Excepion",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }


    @GetMapping("/getCategoryArticle")
    public String getCategoryArticle(@RequestParam("rows") Integer rows,@RequestParam("pageNum") Integer pageNum,
                                     @RequestParam(value = "category")String categoryName){
        try{
            //获取分类文章信息
            DataMap data = categoryService.getCategoryArticle(rows,pageNum,categoryName);

            return JsonResult.build(data).toJSON();
        }catch (Exception e){
            log.error("CategoryController findAllCategoriesName Excepion",e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }
}
